package com.tanhua.recommend.listener;

import com.alibaba.fastjson.JSON;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.mongo.PublishScore;
import com.tanhua.dubbo.api.PublishApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RocketMQMessageListener(topic = "tanhua-quanzi", consumerGroup = "tanhua-quanzi-group")
@Slf4j
public class PublishScoreListener implements RocketMQListener<String> {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Reference
    private PublishApi publishApi;

    /**
     * 1、转换map
     * 2、获取参数(userId,publishId,pid,type)
     * 3、构造对象 publishScore
     * 4、根据不同的type，设置不同的评分
     * 5、保存到mongoDB中
     *
     * @param s
     */
    @Override
    public void onMessage(String s) {
        try {
            //转换map集合
            Map<String, Object> map = JSON.parseObject(s, Map.class);

            //获取参数(userId,publishId,pid,type)
            Long userId = (Long) map.get("userId");
            String publishId = (String) map.get("publishId");
            Long pid = (Long) map.get("pid");
            Integer type = (Integer) map.get("type");

            //构造对象publishScore
            PublishScore publishScore = new PublishScore();
            publishScore.setUserId(userId);
            publishScore.setPublishId(pid);
            publishScore.setDate(System.currentTimeMillis());

            //根据不同的type，设置不同的评分
            //1-发动态，2-浏览动态，3-点赞，4-喜欢，5-评论，6-取消点赞，7-取消喜欢
            switch (type) {
                case 1:
                    double score = 0;
                    Publish publish = publishApi.findById(publishId);
                    if (StringUtils.length(publish.getTextContent()) < 50) {
                        score += 1;
                    } else if (StringUtils.length(publish.getTextContent()) >= 50 && StringUtils.length(publish.getTextContent()) <= 100) {
                        score += 2;
                    } else {
                        score += 3;
                    }
                    publishScore.setScore(score);
                    break;
                case 2:
                    publishScore.setScore(1D);
                    break;
                case 3:
                    publishScore.setScore(5D);
                    break;
                case 4:
                    publishScore.setScore(8D);
                    break;
                case 5:
                    publishScore.setScore(10D);
                    break;
                case 6:
                    publishScore.setScore(-5D);
                    break;
                case 7:
                    publishScore.setScore(-8D);
                    break;
                default:
                    publishScore.setScore(0D);
            }

            mongoTemplate.save(publishScore);
        } catch (Exception e) {
            log.info("消费消息错误，错误信息：" + e);
        }

    }
}
